using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class Camera2D : GameComponent
    {

        protected float _scale;
        public Vector2 _position;
        protected float _viewportHeight;
        protected float _viewportWidth;


        public Camera2D(Game game)
            : base(game)
        {
            Initialize();
            Bound = new Rectangle();
        }

        public override void Initialize()
        {
            _viewportWidth = GraphicsDeviceManager.DefaultBackBufferWidth;
            _viewportHeight = GraphicsDeviceManager.DefaultBackBufferHeight;

            ScreenCenter = new Vector2(_viewportWidth / 2, _viewportHeight / 2);
            Scale = 1;
            MoveSpeed = 1.25f;
            Position = ScreenCenter;
            base.Initialize();
        }


        public Vector2 ScreenCenter
        {
            get;
            protected set;
        }

        public bool IsFocus = true;

        public float CameraTransfer { get; set; }

        public Rectangle Bound { get; set; }

        public float MoveSpeed { get; set; }

        public Focusable Focus { get; set; }

        public void Update(int delta)
        {
            // delta = 50;
            Transform = Matrix.Identity *
                        Matrix.CreateTranslation(-Position.X, -Position.Y, 0)
                       * Matrix.CreateRotationZ(Rotation) *
                        Matrix.CreateTranslation(Origin.X, Origin.Y, 0)
                * Matrix.CreateScale(new Vector3(Scale, Scale, 0))
                        ;

            Origin = ScreenCenter;


            if (IsFocus && Focus != null && Bound != null)
            {

                if (Bound.Left < Focus.FocusPosition.X && Bound.Right > Focus.FocusPosition.X || Bound.IsEmpty)
                    _position.X += (Focus.FocusPosition.X - Position.X) * MoveSpeed * delta / 1000;
                if (Bound.Top < Focus.FocusPosition.Y && Bound.Bottom > Focus.FocusPosition.Y || Bound.IsEmpty)
                    //if (Math.Abs(Focus.FocusPosition.Y - _position.Y - 100) > 50)
                    _position.Y += (Focus.FocusPosition.Y - Position.Y - 100 + CameraTransfer) * MoveSpeed * delta / 1000;
                ///   Vector2 pos = new Vector2();
            }


        }

        public Matrix Transform
        {
            get;
            set;
        }

        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                if ((Bound.Left < value.X && Bound.Right > value.X || Bound.IsEmpty)
                    && (Bound.Top < value.Y && Bound.Bottom > value.Y || Bound.IsEmpty))
                    _position = value;
            }
        }

        public float Rotation
        {
            get;
            set;
        }

        public float Scale
        {
            get
            {
                return _scale;
            }
            set
            {
                if (value <= 0)
                    _scale = 0.001f;
                else
                    _scale = value;
            }
        }

        public Vector2 Origin
        {
            get;
            set;
        }

    }
}
